package org;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import database.DatabaseHelper;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;


/**
 * @author 10107896
 * 
 */
public class Globals {

	public static SQLiteDatabase sqlDB;

	public static Boolean performingPendingRequest = false;

	public static SharedPreferences sharedPrefs;
	public static SharedPreferences.Editor sharedPrefsEditor;

	public static Boolean isLocked = false;
	public static String pin;

	public static String dateAndTime = "EEE. MMM dd, yyyy, hh:mmaa";

	public static String trackingId = "-1";
	
	public static Date lastPauseTime = null;
	
	public static Boolean oldUser=false;

	public static String packageName(Context ctx) {
		try {
			PackageManager pm = ctx.getPackageManager();
			PackageInfo packageInfo = pm
					.getPackageInfo(ctx.getPackageName(), 0);
			return packageInfo.packageName;
		} catch (NameNotFoundException e) {
			return "";
		}
	}

	public static void initialize(Activity act) {

		// Initialize DB
				DatabaseHelper myDbHelper = DatabaseHelper.getInstance(act);

				try {
					sqlDB = myDbHelper.openDataBase();
					createTables(sqlDB);
				} catch (Exception ex) {
					ex.printStackTrace();
				}
	}


	private static void createTables(SQLiteDatabase sqlDB) {
		sqlDB.execSQL(sqlStatements.createDrawingsTable);
		sqlDB.execSQL(sqlStatements.createLayersTable);
		sqlDB.execSQL(sqlStatements.createDrawCMDTable);
		
	}

	public static void deleteDB(Context c){
		sqlDB.close();
		boolean t=c.deleteDatabase(DatabaseHelper.DB_NAME);
		return;
	}


	public static boolean pushEnabled = true;

	public static Date stringToDate(String strDate) {
		SimpleDateFormat formatter;
		formatter = new SimpleDateFormat("EEE. MMM dd, yyyy");
		try {
			return (Date) formatter.parse(strDate);
		} catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static String dateToString(Date strDate) {
		SimpleDateFormat formatter;
		formatter = new SimpleDateFormat("EEE. MMM dd, yyyy");
		return (String) formatter.format(strDate);
	}

	public static String timeToString(Date strDate) {
		SimpleDateFormat formatter;
		formatter = new SimpleDateFormat("hh:mm aa");
		return (String) formatter.format(strDate);
	}

	public static Date stringToTime(String strTime) {
		SimpleDateFormat formatter;
		formatter = new SimpleDateFormat("hh:mm aa");
		try {
			return (Date) formatter.parse(strTime);
		} catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static Date stringToDate(String strDate, String format) {
		SimpleDateFormat formatter;
		formatter = new SimpleDateFormat(format);
		try {
			return (Date) formatter.parse(strDate);
		} catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static String dateToString(Date strDate, String format) {
		SimpleDateFormat formatter;
		formatter = new SimpleDateFormat(format);
		return (String) formatter.format(strDate);
	}


}
